//+--------------------------------------------------------------------------------
//| Copyright (c) 2012 - 2013 ### an.vuongngocduy
//| This file is a part of Compact Game Engine (CGE)
//+--------------------------------------------------------------------------------
#ifndef __CGE_ALLOCATOR_H__
#define __CGE_ALLOCATOR_H__

#include <new.h>
#include "cgeCommons.h"

namespace cge
{
	namespace std
	{
		//! Simple allocator template class. Used to manage memory allocation / deallocation. 
		template<class TVal>
		class Allocator
		{
		public:
			//! Quickly bind current allocator to another type
			template<class TReb>
			struct Rebind { typedef Allocator<TReb> Other; };


		public:
			//! Default constructor
			Allocator(){}


			//! Destructor
			~Allocator(){}


			//! Return maximum number of objects that could be allocated
			u32 MaxSize() 
			{ 
				return ((u32)0xFFFFFFFF) / sizeof(TVal); 
			}


			//! Return object's address
			TVal* Address(TVal& obj)
			{
				return &obj;
			}


			//! Allocate memory for array of objects
			TVal* Allocate(u32 count)
			{
				return (TVal*) ::operator new (sizeof(TVal) * count);
			}

			//! Allocate specified size memory blocks for array of objects
			TVal* Allocate(u32 size, u32 count) 
			{
				return (TVal*) ::operator new (size * count);
			}


			//! Release allocated memory for array of objects
			virtual void Deallocate(TVal* ptr)
			{
				::operator delete(ptr);
			}


			//! Construct an object
			virtual void Construct(TVal* ptr, const TVal& val)
			{
				new ((void*)ptr) TVal(val);
			}


			//! Destroy an object
			virtual void Destroy(TVal* ptr)
			{
				ptr->~TVal();
			}
		};
	}
}

#endif//__CGE_ALLOCATOR_H__